package apresentacaoSwing;

import java.awt.FlowLayout;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import negocio.Cliente;
import negocio.GerenteCliente;
import negocio.ImobiliariaException;


public class TelaNovoCliente {
	
	private TelaNovoCliente frmPrincipal;
	private JFrame frmJanela;
	
	private JTextField tfNome;
	private JTextField tfDataNasc;
	private JTextField tfEndereco;
	private JTextField tfCpf;
	private JTextField tfTelefone;
	private JTextField tfRg;
	private JTextField tfEmail;
	private JTextField tfEstadoCivil;
	private JTextField tfRenda;
	
	private GerenteCliente gerCliente;
	private Cliente cliente;
	private TelaCliente telaC;
	
	public TelaNovoCliente(TelaCliente telaC, GerenteCliente gerCliente) {
		this.gerCliente = gerCliente;
		this.telaC = telaC;
		montaJanela();
	}
	
	public void mostrar() {
		frmJanela.setVisible(true);
	}
	
	private void montaJanela() {
		
		frmJanela = new JFrame(cliente == null ? "Adicionar Cliente" : "Alterar Cliente");
		frmJanela.setSize(650,400);
		frmJanela.setLocationRelativeTo(null);
		frmJanela.setLayout(new GridLayout(10, 1));
		frmJanela.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frmJanela.setResizable(false);
		
		JPanel conteudo = new JPanel(new FlowLayout(FlowLayout.LEFT));
		conteudo.add(new JLabel("Nome: "));
		conteudo.add(tfNome = new JTextField(25));
		frmJanela.add(conteudo);
		
		conteudo = new JPanel(new FlowLayout(FlowLayout.LEFT));
		conteudo.add(new JLabel("Data de Nascimento: "));
		conteudo.add(tfDataNasc = new JTextField(25));
		frmJanela.add(conteudo);
		
		conteudo = new JPanel(new FlowLayout(FlowLayout.LEFT));
		conteudo.add(new JLabel("Endereco: "));
		conteudo.add(tfEndereco = new JTextField(25));
		frmJanela.add(conteudo);
		
		conteudo = new JPanel(new FlowLayout(FlowLayout.LEFT));
		conteudo.add(new JLabel("CPF: "));
		conteudo.add(tfCpf = new JTextField(25));
		frmJanela.add(conteudo);
		
		conteudo = new JPanel(new FlowLayout(FlowLayout.LEFT));
		conteudo.add(new JLabel("Telefone: "));
		conteudo.add(tfTelefone = new JTextField(25));
		frmJanela.add(conteudo);
		
		conteudo = new JPanel(new FlowLayout(FlowLayout.LEFT));
		conteudo.add(new JLabel("RG: "));
		conteudo.add(tfRg = new JTextField(25));
		frmJanela.add(conteudo);
		
		conteudo = new JPanel(new FlowLayout(FlowLayout.LEFT));
		conteudo.add(new JLabel("E-mail: "));
		conteudo.add(tfEmail = new JTextField(25));
		frmJanela.add(conteudo);
		
		conteudo = new JPanel(new FlowLayout(FlowLayout.LEFT));
		conteudo.add(new JLabel("Estado Civil: "));
		conteudo.add(tfEstadoCivil = new JTextField(25));
		frmJanela.add(conteudo);
		
		conteudo = new JPanel(new FlowLayout(FlowLayout.LEFT));
		conteudo.add(new JLabel("Renda: "));
		conteudo.add(tfRenda = new JTextField(25));
		frmJanela.add(conteudo);
		
		conteudo = new JPanel(new FlowLayout(FlowLayout.CENTER));
		JButton btAdicionar = new JButton(cliente == null ? "Adicionar" : "Alterar");
		btAdicionar.addActionListener(new ManipuladorAdicionar());
		conteudo.add(btAdicionar);
		JButton btCancelar = new JButton("Cancelar");
		btCancelar.addActionListener(
				new ActionListener() {

					public void actionPerformed(ActionEvent e) {

						frmJanela.dispose();
						
					}
					
				});
		conteudo.add(btCancelar);
		frmJanela.add(conteudo);		
	}

	public JFrame getFrmJanela() {
		return frmJanela;
	}
	public void setFrmJanela(JFrame frmJanela) {
		this.frmJanela = frmJanela;
	}

	private class ManipuladorAdicionar implements ActionListener  {
		
		private GerenteCliente gerCli;			
		public void actionPerformed(ActionEvent e) {
			
			try {
					Cliente c = new Cliente();
							c.setNome(tfNome.getText());
							c.setDataNasc(tfDataNasc.getText());
							c.setEndereco(tfEndereco.getText());
							c.setCpf(tfCpf.getText());
							c.setTelefone(tfTelefone.getText());
							c.setRg(tfRg.getText());
							c.setEmail(tfEmail.getText());
							c.setEstadoCivil(tfEstadoCivil.getText());
							c.setRenda(Float.parseFloat(tfRenda.getText()));
				
						gerCli.adicionar(c);
			
						
							
				}
				
					catch(ImobiliariaException a){
            JOptionPane.showMessageDialog(null, a.getMessage(),
                    "Erro ao incluir novo cliente",
                    JOptionPane.ERROR_MESSAGE);
					}
				
					catch(NumberFormatException exc) {
				JOptionPane.showMessageDialog(null,
						"Numero ou data invalida!!!",
						"Erro ao adicionar cliente",
						JOptionPane.ERROR_MESSAGE);} 	
		}
	}
}